package experiment4;

// 有理数类
class RationalNumber {
    private int numerator;   // 分子
    private int denominator; // 分母

    // 构造方法：初始化并通分
    public RationalNumber(int numerator, int denominator) {
        if (denominator == 0) {
            throw new IllegalArgumentException("分母不能为0");
        }
        // 计算最大公约数进行约分
        int gcd = gcd(Math.abs(numerator), Math.abs(denominator));
        this.numerator = numerator / gcd;
        this.denominator = denominator / gcd;
        // 确保分母为正
        if (this.denominator < 0) {
            this.numerator = -this.numerator;
            this.denominator = -this.denominator;
        }
    }

    // 计算最大公约数
    private int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    // 加法
    public RationalNumber add(RationalNumber other) {
        int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator;
        int newDenominator = this.denominator * other.denominator;
        return new RationalNumber(newNumerator, newDenominator);
    }

    // 减法
    public RationalNumber subtract(RationalNumber other) {
        int newNumerator = this.numerator * other.denominator - other.numerator * this.denominator;
        int newDenominator = this.denominator * other.denominator;
        return new RationalNumber(newNumerator, newDenominator);
    }

    // 乘法
    public RationalNumber multiply(RationalNumber other) {
        int newNumerator = this.numerator * other.numerator;
        int newDenominator = this.denominator * other.denominator;
        return new RationalNumber(newNumerator, newDenominator);
    }

    // 除法
    public RationalNumber divide(RationalNumber other) {
        if (other.numerator == 0) {
            throw new ArithmeticException("除数不能为0");
        }
        int newNumerator = this.numerator * other.denominator;
        int newDenominator = this.denominator * other.numerator;
        return new RationalNumber(newNumerator, newDenominator);
    }

    // 重写toString
    @Override
    public String toString() {
        if (denominator == 1) {
            return String.valueOf(numerator);
        }
        return numerator + "/" + denominator;
    }

    // Getter方法
    public int getNumerator() {
        return numerator;
    }

    public int getDenominator() {
        return denominator;
    }
}

// 有理数测试类
public class test1 {
    public static void main(String[] args) {
        RationalNumber r1 = new RationalNumber(2, 4); // 1/2
        RationalNumber r2 = new RationalNumber(3, 6); // 1/2

        System.out.println("r1 = " + r1);
        System.out.println("r2 = " + r2);

        RationalNumber add = r1.add(r2);
        System.out.println("r1 + r2 = " + add); // 1

        RationalNumber sub = r1.subtract(r2);
        System.out.println("r1 - r2 = " + sub); // 0

        RationalNumber mul = r1.multiply(r2);
        System.out.println("r1 * r2 = " + mul); // 1/4

        RationalNumber div = r1.divide(r2);
        System.out.println("r1 / r2 = " + div); // 1
    }
}
